var express = require('express');
var router = express.Router();
const query = require('./mysql');
const tokenApi = require('../token_vertify');
const encrypt = require('./util/encrypt.js');
/* post login listing. */
router.post('/', async function (req, res, next) {
	console.log(req.body);
	let user = req.body,
		sqlStr = 'select * from user where account = ? and password = ?',
		sqlArr = [user.username, encrypt(user.password)];
	let result = await query(sqlStr, sqlArr);
	console.log(result);

	if (result.length == 0) {
		return res.send({
			status: 404,
			message: '用户名或密码输入错误',
		});
	}
	if (result[0].isLogin !== '1') {
		return res.send({
			status: 404,
			message: '等待管理批准',
		});
	}
	tokenApi.setToken(user.username).then((token) => {
		res.send({
			status: 200,
			message: '登录成功',
			token,
			data: result,
		});
	});
});
module.exports = router;
